{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit Diagnostics;

interface

uses Classes, Canvas, AHMTypes, Colour, ConsoleManager;

type
  TAHMDiagnostics = class
  private
    FDiagFont: TAHMCanvasFont;
    FDiagColour: TAHMCanvasColour;
    FDiagShadow: TAHMCanvasColour;
    FWatchColour: TAHMCanvasColour;
    FWatchShadow: TAHMCanvasColour;
    FWatchFont: TAHMCanvasFont;
    FBackground: TAHMCanvasColour;
    FConsole: TAHMConsoleManager;
  protected
  public
    constructor Create;
    destructor Destroy; override;
    property Console: TAHMConsoleManager read FConsole;
    procedure Render;
  end;

implementation

uses ApplicationManager, Screen;

constructor TAHMDiagnostics.Create;
begin
  inherited Create;

  // Create colour object to render diagnostic colour
  FDiagColour  := TAHMCanvasColour.CreateARGB(255, 255, 240, 255);
  FDiagShadow  := TAHMCanvasColour.CreateARGB(255, 0, 0, 0);
  FWatchColour := TAHMCanvasColour.CreateARGB(255, 240, 240, 255);
  FWatchShadow := TAHMCanvasColour.CreateARGB(255, 0, 0, 0);
  FBackground  := TAHMCanvasColour.CreateARGB(60, 0, 0, 255);

  // Create our font object to render diagnostic text
  FDiagFont := TAHMCanvasFont.Create;
  FDiagFont.Typeface := 'Lucida Console';
  FDiagFont.Size := 14;
  FDiagFont.Colour := FDiagColour;
  FDiagFont.Shadow := True;
  FDiagFont.ShadowXOffset := 1;
  FDiagFont.ShadowYOffset := 1;
  FDiagFont.ShadowColour := FDiagShadow;

  // Create our font object to render watch display
  FWatchFont := TAHMCanvasFont.Create;
  FWatchFont.Typeface := 'Lucida Console';
  FWatchFont.Size := 20;
  FWatchFont.Colour := FWatchColour;
  FWatchFont.Shadow := True;
  FWatchFont.ShadowXOffset := 2;
  FWatchFont.ShadowYOffset := 2;
  FWatchFont.ShadowColour := FWatchShadow;

  // Create console manager
  FConsole := TAHMConsoleManager.Create;
end;

destructor TAHMDiagnostics.Destroy;
begin
  // Free font & colour objects
  FDiagFont.Free;
  FWatchFont.Free;
  FDiagColour.Free;
  FDiagShadow.Free;
  FWatchColour.Free;
  FWatchShadow.Free;
  FBackground.Free;

  // Free console manager
  FConsole.Free;

  inherited Destroy;
end;

procedure TAHMDiagnostics.Render;
var
  TextRect: TAHMRectF;
begin
  // Draw diagnostic information on main viewport canvas
  with ScreenInstance.Viewport do
  begin
    // If main diagnostic display is enabled then write our output & command line
    if ApplicationManagerInstance.UIManager.DisplayDiagnostics then
    begin
      Canvas.UnlockRotation;
      Canvas.UnSetRotation;
      TextRect := ViewportRect;
      Canvas.BrushColour := FBackground;
      Canvas.FillRect(TextRect, nil, nil, 1.0, 1.0);

      // Use top 95% for rendering logs / feedback
      TextRect.Height := TextRect.Height * 0.95;
      Canvas.TextRect(TextRect, FDiagFont, FConsole.CommandOutput, taLeft, True, nil, nil, nil, 1.0, 1.0, False, False);

      // Use remainder for rendering command line
      TextRect.Y := TextRect.Height + 0.01;
      TextRect.Height := ViewportRect.Height - TextRect.Y;
      Canvas.TextRect(TextRect, FDiagFont, FConsole.CommandLine, taLeft, False, nil, nil, nil, 1.0, 1.0, False, False);
    end;

    // If console watches are enabled then overlay them
    if FConsole.WatchesEnabled then
    begin
      Canvas.UnlockRotation;
      Canvas.UnSetRotation;
      TextRect := ViewportRect;
      Canvas.TextRect(TextRect, FWatchFont, FConsole.WatchesOutput, taLeft, True, nil, nil, nil, 1.0, 1.0, False, False);
    end;
  end;
end;


end.
